<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>车辆二维码管理</title>
    <style>
        :root {
            --primary-color: #337ab7;
            /* A simple, standard blue */
            --font-color: #333;
            --border-color: #ddd;
            --background-color: #f5f5f5;
            --white-color: #ffffff;
            --hover-color: #f9f9f9;
            --shadow-color: rgba(0, 0, 0, 0.05);
            /* More subtle shadow */
        }

        body {
            font-family: Arial, sans-serif;
            /* Simpler font stack */
            background-color: var(--background-color);
            margin: 0;
            padding: 15px;
            /* Reduced padding */
            color: var(--font-color);
            font-size: 14px;
            /* Slightly smaller base font */
        }

        .container {
            max-width: 1100px;
            margin: 0 auto;
            background-color: var(--white-color);
            padding: 20px;
            /* Reduced padding */
            border-radius: 8px;
            /* Less rounded */
            box-shadow: 0 2px 8px var(--shadow-color);
            /* Reduced shadow */
            border: 1px solid var(--border-color);
            /* Add a border for definition */
        }

        h1 {
            color: var(--font-color);
            /* Plainer color */
            text-align: center;
            margin-top: 0;
            margin-bottom: 25px;
            font-weight: 600;
            font-size: 24px;
        }

        .toolbar {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 20px;
        }

        .search-box {
            width: 250px;
            padding: 8px 12px;
            /* Reduced padding */
            border-radius: 4px;
            /* Less rounded */
            border: 1px solid var(--border-color);
            font-size: 14px;
        }

        .car-table {
            width: 100%;
            border-collapse: collapse;
            text-align: left;
        }

        .car-table th,
        .car-table td {
            padding: 10px 12px;
            /* Reduced padding */
            border-bottom: 1px solid var(--border-color);
            vertical-align: middle;
        }

        .car-table th {
            background-color: var(--hover-color);
            /* Lighter header */
            font-weight: 600;
        }

        .car-table tbody tr:last-child td {
            border-bottom: none;
        }

        .car-table tbody tr:hover {
            background-color: var(--hover-color);
        }

        .pagination {
            margin-top: 25px;
            text-align: center;
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
        }

        .pagination ul {
            list-style: none;
            padding: 0;
            margin: 0;
            display: inline-flex;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            overflow: hidden;
        }

        .pagination li {
            display: inline;
        }

        .pagination a,
        .pagination span {
            display: inline-block;
            padding: 8px 12px;
            text-decoration: none;
            color: var(--font-color);
            background-color: var(--white-color);
            border-right: 1px solid var(--border-color);
            transition: background-color 0.3s;
        }

        .pagination li:last-child a,
        .pagination li:last-child span {
            border-right: none;
        }

        .pagination a:hover {
            background-color: #e9e9e9;
        }

        .pagination .active span {
            background-color: var(--primary-color);
            color: var(--white-color);
            border-color: var(--primary-color);
            cursor: default;
        }

        .pagination .disabled span {
            color: #999;
            cursor: not-allowed;
            background-color: var(--hover-color);
        }

        .btn {
            display: inline-block;
            padding: 6px 14px;
            border: 1px solid transparent;
            border-radius: 4px;
            color: var(--white-color);
            background-color: var(--primary-color);
            cursor: pointer;
            text-decoration: none;
            font-size: 14px;
            transition: all 0.3s ease;
            margin-right: 5px;
        }

        .btn:hover {
            opacity: 0.85;
        }

        .btn-download {
            background-color: #5bc0de;
        }

        .qrcode-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.6);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }

        .qrcode-modal-content {
            background-color: var(--white-color);
            padding: 25px;
            border-radius: 8px;
            text-align: center;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
        }

        .qrcode-modal img {
            max-width: 250px;
            height: auto;
            border: 1px solid var(--border-color);
        }

        .qrcode-modal h3 {
            margin-top: 0;
            margin-bottom: 20px;
        }

        .qrcode-modal .close-btn {
            position: absolute;
            top: 15px;
            right: 15px;
            font-size: 24px;
            color: #aaa;
            cursor: pointer;
        }

        /* Responsive adjustments for mobile */
        @media (max-width: 768px) {
            body {
                padding: 10px;
                font-size: 13px;
            }

            .container {
                padding: 15px;
                border: none;
                box-shadow: none;
            }

            .search-box {
                width: 100%;
            }

            .car-table {
                display: block;
                width: 100%;
            }

            .car-table thead {
                display: none;
                /* Hide table headers on mobile */
            }

            .car-table tbody,
            .car-table tr,
            .car-table td {
                display: block;
                width: 100%;
                box-sizing: border-box;
            }

            .car-table tr {
                margin-bottom: 10px;
                border: 1px solid var(--border-color);
                border-radius: 8px;
                overflow: hidden;
            }

            .car-table td {
                padding: 8px 12px;
                border-bottom: 1px solid #f0f0f0;
                text-align: right;
                position: relative;
            }

            .car-table td:last-child {
                border-bottom: none;
            }

            .car-table td::before {
                content: attr(data-label);
                position: absolute;
                left: 12px;
                font-weight: bold;
                color: #555;
            }

            .pagination {
                padding: 0;
            }

            .pagination a,
            .pagination span {
                padding: 8px 10px;
            }

            .btn {
                padding: 8px 12px;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>车辆二维码管理</h1>
        <div class="toolbar">
            <form action="" method="get">
                <input type="text" name="search" class="search-box" placeholder="输入车牌号搜索..." value="{$search ?? ''}">
                <button type="submit" class="btn" style="display: inline-block; margin-left: 10px;">搜索</button>
            </form>
        </div>
        <table class="car-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>车牌号</th>
                    <th>卡类型</th>
                    <th>ID卡</th>
                    <th>型号</th>

                    <th>门店</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="carTableBody">
                {volist name="carList" id="car"}
                <tr>
                    <td data-label="ID">{$car.id}</td>
                    <td data-label="车牌号">{$car.CarNomber}</td>
                    <td data-label="卡类型">{$car.IDCardType}</td>
                    <td data-label="ID卡">{$car.IDCardNomber}</td>
                    <td data-label="计费型号">{$car.JiFeiXiangName}</td>
                    <td data-label="门店">{$car.ShopName}</td>
                    <td data-label="操作">
                        <button class="btn" onclick="showQrCode('{$car.IDCardNomber}','{$car.CarNomber}')">生成/显示二维码</button>
                    </td>
                </tr>
                {/volist}
            </tbody>
        </table>
        <div class="pagination" id="pagination">
            {$page|raw}
        </div>
    </div>

    <div id="qrcodeModal" class="qrcode-modal" style="display: none;" onclick="closeModal()">
        <div class="qrcode-modal-content" onclick="event.stopPropagation()">
            <span class="close-btn" onclick="closeModal()">&times;</span>
            <h3 id="modalCarNumber"></h3>
            <img id="qrcodeImage" src="" alt="二维码">
            <br><br>
            <a id="downloadBtn" class="btn btn-download" href="#" download>下载二维码</a>
        </div>
    </div>

    <script>
        const modal = document.getElementById('qrcodeModal');
        const qrcodeImage = document.getElementById('qrcodeImage');
        const modalCarNumber = document.getElementById('modalCarNumber');
        const downloadBtn = document.getElementById('downloadBtn');

        async function showQrCode(id_card_num,carNumber) {
            modalCarNumber.textContent = `车牌号: ${carNumber}`+'\r\n'+`ID号: ${id_card_num}`;
            qrcodeImage.src = '/static/loading.gif'; // 清空旧图片
            downloadBtn.style.display = 'none';
            modal.style.display = 'flex';

            try {
                const response = await fetch(`/index.php/car/generateQrcode?car_number=${id_card_num}`);
                const result = await response.json();

                if (result.code === 200) {
                    qrcodeImage.src = result.data.qrcode_url;
                    downloadBtn.href = result.data.qrcode_url;
                    downloadBtn.download = `${result.data.car_number}_${result.data.idcard_number}.png`;
                    downloadBtn.style.display = 'inline-block';
                } else {
                    alert('二维码生成失败: ' + result.msg);
                    closeModal();
                }
            } catch (error) {
                alert('请求失败: ' + error.message);
                closeModal();
            }
        }

        function closeModal() {
            modal.style.display = 'none';
        }
    </script>
</body>

</html>